package cn.jly.spring.annotation.ioc;

import cn.jly.spring.annotation.ioc.config.MainConfigProfile;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;

/**
 * @author lanyangji
 * @description profile测试
 * @date 2020/11/13 下午 6:41
 */
public class IocTestProfile {

    AnnotationConfigApplicationContext applicationContext;

    @Before
    public void setUp() throws Exception {
        // applicationContext = new AnnotationConfigApplicationContext(MainConfigProfile.class);
        applicationContext = new AnnotationConfigApplicationContext();
        // 设置激活的环境  - 优先级高于虚拟机参数
        applicationContext.getEnvironment().setActiveProfiles("dev");
        // 注册配置类
        applicationContext.register(MainConfigProfile.class);
        // 启动刷新容器
        applicationContext.refresh();
    }

    @After
    public void tearDown() throws Exception {
        applicationContext.close();
    }

    @Test
    public void test() {
        final Map<String, ComboPooledDataSource> map = applicationContext.getBeansOfType(ComboPooledDataSource.class);
        for (Map.Entry<String, ComboPooledDataSource> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "-->" + entry.getValue());
        }
    }
}
